package Tree116;
import java.util.*;
public class AffterTree {
    public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
      this.val = val;
    }
  }
    public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
        ArrayList<ArrayList<Integer>> p=new ArrayList<ArrayList<Integer>>();
        if(root==null){
            return p;
        }

        Queue<TreeNode> q=new ArrayDeque<TreeNode>();
        TreeNode cur=root;
        q.add(cur);
        while(!q.isEmpty()){
            ArrayList<Integer> list=new ArrayList<>();
            int n=q.size();
            for(int i=0;i<n;i++){
                TreeNode w=q.poll();
                list.add(w.val);
                if(w.left!=null){
                    q.add(w.left);
                }
                if(w.right!=null){
                    q.add(w.right);
                }
            }
            p.add(list);
        }
        return p;
    }
    //求树高度
    public class Solution {

        public int maxDepth (TreeNode root) {

            if(root==null){
                return 0;
            }
            int n1=maxDepth(root.left);
            int n2=maxDepth(root.right);
            return Math.max(n1,n2)+1;
        }
    }
}
